{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "67541c63",
   "metadata": {},
   "source": [
    "## 第三周课堂笔记\n",
    "* 记录人：周晓源\n",
    "* 课程名称：API、机器学习与人工智能\n",
    "* 教师：许智超\n",
    "* 课后参考： [老师的gitee](https://gitee.com/xzhichao/api_ai_ml/blob/master/week03/week03_CV_face_%E5%91%A8%E4%B8%80%E7%8F%AD%E8%AF%BE%E5%A0%82%E7%AC%94%E8%AE%B0.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b73a5cd",
   "metadata": {},
   "source": [
    "#  Python 使用API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b8c8f834",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T11:45:15.219969Z",
     "start_time": "2022-03-21T11:45:15.106533Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8ce6409",
   "metadata": {},
   "source": [
    "# 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "be240b3a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:26:37.299808Z",
     "start_time": "2022-03-21T12:26:36.754436Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.API账户/通行证\n",
    "API_key = 'UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P'\n",
    "API_sercret = 'uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "#4. 准备playload(酬载)\n",
    "payload = {\n",
    "    'api_key': API_key,\n",
    "    'api_secret': API_sercret,\n",
    "    'image_url': 'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "# 4. 发起请求\n",
    "\n",
    "r = requests.post(base_url, params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "12db9cc5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:19:04.498522Z",
     "start_time": "2022-03-21T12:19:04.483534Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cd375fda",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:26:39.000488Z",
     "start_time": "2022-03-21T12:26:38.994488Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647865597,3add436a-3280-4cef-a4e4-ae22a3cafcb2',\n",
       " 'time_used': 198,\n",
       " 'faces': [{'face_token': 'acf3a668ae75265e2c3815829c8b98db',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83bc19b2",
   "metadata": {},
   "source": [
    "# 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4e31b66a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:32:39.371246Z",
     "start_time": "2022-03-21T12:32:38.970839Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1. url\n",
    "analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P'\n",
    "API_sercret = 'uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv'\n",
    "\n",
    "# 3. post\n",
    "\n",
    "# 4. 权限：所有API Key 都可以调用本API\n",
    "\n",
    "# 5. payload：酬载，必要阅读api文档中的  必选项  和  可选项  ，以及  参数说明\n",
    "payload = {\n",
    "    'api_key': API_key,\n",
    "    'api_secret': API_sercret,\n",
    "    'face_tokens':'b3ddf560951d58fad20b864131ba8f71',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    }\n",
    "r_a = requests.post(analyze_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "eef08437",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:32:40.447360Z",
     "start_time": "2022-03-21T12:32:40.430232Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3611cff6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:32:52.258893Z",
     "start_time": "2022-03-21T12:32:52.247867Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 122,\n",
       " 'request_id': '1647865959,b2a2c014-3b60-4f59-a14d-1a3f9044eb3b',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.039,\n",
       "     'neutral': 0.001,\n",
       "     'disgust': 0.191,\n",
       "     'anger': 0.001,\n",
       "     'surprise': 0.031,\n",
       "     'fear': 0.001,\n",
       "     'happiness': 99.736},\n",
       "    'beauty': {'female_score': 58.555, 'male_score': 58.212},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'mouthstatus': {'close': 0.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 100.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 1.554,\n",
       "     'stain': 9.17,\n",
       "     'acne': 1.527,\n",
       "     'health': 11.352},\n",
       "    'headpose': {'yaw_angle': 2.7483263,\n",
       "     'pitch_angle': 14.900063,\n",
       "     'roll_angle': 0.5866398},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.376},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.376},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.376}},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.005,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.995,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.003,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.001,\n",
       "      'no_glass_eye_open': 99.996,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 85.357},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.494,\n",
       "      'vector_z_component': 0.935,\n",
       "      'vector_x_component': -0.068,\n",
       "      'vector_y_component': 0.348,\n",
       "      'position_y_coordinate': 0.438},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.502,\n",
       "      'vector_z_component': 0.942,\n",
       "      'vector_x_component': 0.247,\n",
       "      'vector_y_component': 0.227,\n",
       "      'position_y_coordinate': 0.422}}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': 'b3ddf560951d58fad20b864131ba8f71'}]}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_a.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7ab73620",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:26:57.771340Z",
     "start_time": "2022-03-21T12:26:57.761303Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'acf3a668ae75265e2c3815829c8b98db'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()['faces'][0]['face_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a81c7af",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "458a9315",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:49:50.316200Z",
     "start_time": "2022-03-21T12:49:49.861016Z"
    }
   },
   "outputs": [],
   "source": [
    "#  另一张图片\n",
    "# 1.url\n",
    "base1_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.API账户/通行证\n",
    "API_key = 'UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P'\n",
    "API_sercret = 'uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "#4. 准备playload(酬载)\n",
    "payload = {\n",
    "    'api_key': API_key,\n",
    "    'api_secret': API_sercret,\n",
    "    'image_url': 'https://img0.baidu.com/it/u=877682596,1742403669&fm=253&fmt=auto&app=138&f=JPEG?w=572&h=421'\n",
    "}\n",
    "\n",
    "# 4. 发起请求\n",
    "\n",
    "r2 = requests.post(base1_url, params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "f50b9edc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:49:53.801084Z",
     "start_time": "2022-03-21T12:49:53.783022Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abd6ec5711ef1c69702d01cc7223b96a'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r2.json()['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "49a68883",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:53:32.217738Z",
     "start_time": "2022-03-21T12:53:31.469705Z"
    }
   },
   "outputs": [],
   "source": [
    "# 人脸对比\n",
    "# 1.url\n",
    "base2_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "# 2.API账户/通行证\n",
    "API_key = 'UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P'\n",
    "API_sercret = 'uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "#4. 准备playload(酬载)\n",
    "payload = {\n",
    "    'api_key': API_key,\n",
    "    'api_secret': API_sercret,\n",
    "    'face_token1':'acf3a668ae75265e2c3815829c8b98db',\n",
    "    'face_token2':'abd6ec5711ef1c69702d01cc7223b96a',\n",
    "    \n",
    "}\n",
    "\n",
    "# 4. 发起请求\n",
    "\n",
    "r3 = requests.post(base2_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "35ff5bb9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T12:53:42.851113Z",
     "start_time": "2022-03-21T12:53:42.835699Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647867212,cb4d9cc7-61b4-4df0-bfab-f7a6ba37d332',\n",
       " 'time_used': 384,\n",
       " 'confidence': 90.402,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r3.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b274ed4f",
   "metadata": {},
   "source": [
    "# 封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "6a6f0686",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T15:22:02.601480Z",
     "start_time": "2022-03-21T15:22:02.588478Z"
    }
   },
   "outputs": [],
   "source": [
    "API_key = 'UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P'\n",
    "API_sercret = 'uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv'\n",
    "img_url_01= 'https://img0.baidu.com/it/u=877682596,1742403669&fm=253&fmt=auto&app=138&f=JPEG?w=572&h=421'\n",
    "img_url_02= 'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "78b625f3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T15:24:27.753939Z",
     "start_time": "2022-03-21T15:24:27.742941Z"
    }
   },
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    '''face++ API人脸检测函数'''\n",
    "    base3_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "            'api_key': API_key,\n",
    "            'api_secret': API_sercret,\n",
    "            'image_url':img_url\n",
    "    }\n",
    "    r4 = requests.post(base3_url, params = payload)\n",
    "    \n",
    "    return r4.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "777f48b5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T15:24:29.944333Z",
     "start_time": "2022-03-21T15:24:29.307228Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647876269,b5cf3acc-e63c-4625-b733-6ad878f573c5',\n",
       " 'time_used': 392,\n",
       " 'faces': [{'face_token': '375611dd377f62b875c0c610736e0b10',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "2dcbd80d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-21T15:24:38.707950Z",
     "start_time": "2022-03-21T15:24:38.294549Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647876278,d78bd3d7-bcba-409a-8eba-010c2670b94a',\n",
       " 'time_used': 163,\n",
       " 'faces': [{'face_token': 'e2f374567f544a0358f4d8e69693bb66',\n",
       "   'face_rectangle': {'top': 99, 'left': 213, 'width': 141, 'height': 141}}],\n",
       " 'image_id': 'P4jfQiI0Tl6FfwMefrizJw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7199629",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
